﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Avatar.Management;
using Avatar.Model;
using Avatar.Common;
using Avatar.MembershipService;

public partial class Admin_GroupMenu : System.Web.UI.Page
{
    string _gourpName=string.Empty;
    protected void Page_Load(object sender, EventArgs e)
    {
        _gourpName = drp_GroupList.SelectedValue;
        TreeViewShow(Table1, _gourpName);

    }
    protected void TreeViewShow(Table _table, string _gName)
    {
        Table1.Controls.Clear();
        var _accessRole = RoleManager.GetUserAccessRole(_gName);

        var _departments = MenuManager.GetDepartment();
        foreach (var _department in _departments)
        {
            TableRow _row = new TableRow();

            TableCell _cellDepartment = new TableCell();
            _cellDepartment.Text = "<b>"+_department.Name+":</b>";
            _cellDepartment.Width = new Unit("10%");

            var _categorys = MenuManager.GetCategories(_department.ID);

            TableCell _cellCategory = new TableCell();
            foreach (var _category in _categorys)
            {
                Button _cb = new Button();
                _cb.ID = _category.ID.ToString();
                _cb.CommandArgument = _category.ID.ToString();
                _cb.Text = _category.Name;
                _cb.Click += new EventHandler(CheckBox_CheckedChanged);
                _cb.EnableViewState = false;
                foreach (var _r in _accessRole)
                {
                    if (_cb.ID == _r.MenuID.ToString()) _cb.Text = _category.Name + " √";
                }
                _cellCategory.Controls.Add(_cb);
            }

            _row.Cells.Add(_cellDepartment);
            _row.Cells.Add(_cellCategory);

            _table.Rows.Add(_row);
        }
    }
    protected void CheckBox_CheckedChanged(object sender, EventArgs e)
    {
        var _groupName = drp_GroupList.SelectedValue;

        Button _cb = sender as Button;

        RoleManager.UpdateRole(_groupName, Convert.ToInt32(_cb.CommandArgument));
        
        TreeViewShow(Table1, _gourpName);
    }
    protected void drp_GroupList_SelectedIndexChanged(object sender, EventArgs e)
    {
        //TreeViewShow(Table1, Convert.ToInt32(drp_GroupList.SelectedValue););
    }
}
